import React, { memo, useEffect } from 'react'
import { useDispatch } from 'react-redux'
import Header from '@/components/b_header'
import Footer from '@/components/b_footer'
import {
  changeBlogUserAction,
  getUserProfileAction
} from '@/store/blog/actionCreators'
import switchRouter from '@/utils/switchRouter'
import BackTop from '@/components/b_backtop'
import { getUserId } from '@/utils/common'

export default memo(function B_Home(props) {
  const { type } = props.route
  const localUser = JSON.parse(localStorage.getItem('user')) || {}
  const userId = getUserId(props) || localUser.id || 8
  const dispatch = useDispatch()

  useEffect(() => {
    // 保存基本用户信息
    if (userId === localUser.id) {
      // 自己的主页
      dispatch(changeBlogUserAction(localUser))
    }
    userId && dispatch(getUserProfileAction(userId))
  }, [dispatch, localUser, userId])

  return (
    <>
      <Header userId={userId} />
      {switchRouter(type, userId, localUser.id)}
      <Footer />
      <BackTop />
    </>
  )
})
